import dao.impl.MapDaoImpl;
import entity.Dictionary;
import util.TextUtil;

import javax.swing.*;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.List;

/**
 * @author ASUS
 */
public class Map extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField input;
    private JTextArea outPut;
    private JCheckBox queryCheckBox;
    private JCheckBox insertCheckBox;
    private MapDaoImpl dao = new MapDaoImpl();
    {
        Dimension d = input.getPreferredSize();
        input.setPreferredSize(new Dimension((int) d.getWidth(), (int) (d.getHeight()+15)));
    }

    public Map() {
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);
        this.setLocationRelativeTo(null);
        // this.setIconImage(new ImageIcon("src/1.jpg").getImage());
        buttonOK.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        String b = null;

        if(queryCheckBox.isSelected())
        {
            outPut.setText("");
           List<Dictionary> p = null;
            try {
                p = dao.selectValue(input.getText());
            } catch (SQLException e) {
                // e.printStackTrace();
                b = e.toString();
            }finally {
                outPut.append("ok\r\n");
                if(p!=null&& !p.isEmpty())
                {
                    for(Dictionary dic:p)
                    {
                        outPut.append(dic.getValue()+"\r\n");
                    }
                }
            }
        }else{
            String k = input.getText(),v = outPut.getText();
            if((!TextUtil.isEmpty(v )&& (!TextUtil.isEmpty(k))))
            {
                try {
                    outPut.setText("");
                    dao.insertValue(k,v);

                } catch (SQLException e) {
                    b = e.toString();
                }finally {
                    outPut.append("ok");
                }
            }
        }

        if(b != null)
        {
            outPut.setText(b);
        }
    }

    UndoManager undoManager = new UndoManager();
    {
        this.input.getDocument().addUndoableEditListener(undoManager);
        this.outPut.getDocument().addUndoableEditListener(undoManager);
    }
    private void onCancel() {
        // add your code here if necessary
        // dispose();
        if(undoManager.canUndo())
        {
            undoManager.undo();
        }

    }


}
